package com.john.blog.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.john.blog.common.utils.DetectUtils;
import com.john.blog.common.utils.KeyUtils;
import com.john.blog.model.IBaseMapper;
import com.john.blog.model.po.Model;
import com.john.blog.service.BaseService;

import tk.mybatis.mapper.entity.Example;

/**
 * 基础service实现
 * 
 * @author JiangGengchao
 * @date 2018年2月5日
 */
public abstract class BaseServiceImpl<T> implements BaseService<T> {

    protected IBaseMapper<T> mapper;

    @Override
    public List<T> findAll() {
        return this.mapper.selectAll();
    }

    @Override
    public int insert(T record) {
        if (DetectUtils.isEmpty(((Model) record).getId())) {
            ((Model) record).setId(KeyUtils.getUUID());
        }
        return this.mapper.insert(record);
    }

    @Override
    public List<T> selectByExample(Example example) {
        return this.mapper.selectByExample(example);
    }

    @Override
    public PageInfo<T> findPagination(Example example, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.mapper.selectByExample(example));
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return this.mapper.deleteByPrimaryKey(id);
    }

    @Override
    public T updateByPrimaryKeySelective(T record) {
        this.mapper.updateByPrimaryKeySelective(record);
        return this.selectByPrimaryKey(((Model) record).getId());
    }

}
